﻿using ValidationEngine.Commons;
using ValidationEngine.Commons.Enums;
using ValidationEngine.Commons.Interfaces;
using ValidationEngine.Tools;

namespace ValidationEngine.ValidationRules
{
    public class RequiredRule: BaseValidationRule
    {
        protected override IValidationInfo ApplyRule(IValidationContext ctx)
        {
            var propVal = ModelHelper.GetPropertyValue(ctx);
            DefResult = (propVal == null || propVal.ToString().Trim().Length == 0) ? _defErrorMsg : null;
            return Result;
        }

        #region Ctor
        public RequiredRule(
         string defErrorText = null,
         RuleApplingContext ruleApplingContext = RuleApplingContext.Always,
         ValidationErrorType errorType = ValidationErrorType.Error)
        {
            this.ApplingContext = ruleApplingContext;
            this._defErrorType = errorType;
            this._defErrorMsg = defErrorText ?? "This field is required.";
        } 
        #endregion
    }
}